﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PylonLiveView
{
    class CeLang_test
    {
        public HTuple hv_distance = new HTuple();
        public HTuple hv_distance_banchang = new HTuple();

        public void cl(HObject h)
        {


            // Local iconic variables 

            HObject ho_Image20180517163722, ho_ROI_0, ho_ImageReduced1;
            HObject ho_Regions, ho_Rectangle, ho_RegionDilation, ho_ImageAffineTrans1;
            HObject ho_RegionAffineTrans, ho_ImageReduced, ho_ImageInvert;
            HObject ho_ImageFilter, ho_ImageFFT, ho_ImageConvol, ho_ImageFFT1;
            HObject ho_Regions1, ho_Cross, ho_Image;

            // Local control variables 

            HTuple hv_Row = null, hv_Column = null, hv_Phi1 = null;
            HTuple hv_Length1 = null, hv_Length2 = null, hv_Area1 = null;
            HTuple hv_Row2 = null, hv_Column2 = null, hv_Phi2 = null;
            HTuple hv_HomMat2D1 = null, hv_Width = null, hv_Height = null;
            HTuple hv_CameraParameters = null, hv_CameraPose = null;
            HTuple hv_AmplitudeThreshold = null, hv_RoiWidthLen2 = null;
            HTuple hv_LineRowStart_Measure_01_0 = null, hv_LineColumnStart_Measure_01_0 = null;
            HTuple hv_LineRowEnd_Measure_01_0 = null, hv_LineColumnEnd_Measure_01_0 = null;
            HTuple hv_TmpCtrl_Row = null, hv_TmpCtrl_Column = null;
            HTuple hv_TmpCtrl_Dr = null, hv_TmpCtrl_Dc = null, hv_TmpCtrl_Phi = null;
            HTuple hv_TmpCtrl_Len1 = null, hv_TmpCtrl_Len2 = null;
            HTuple hv_MsrHandle_Measure_01_0 = null, hv_LineRowStart_Measure_01_1 = null;
            HTuple hv_LineColumnStart_Measure_01_1 = null, hv_LineRowEnd_Measure_01_1 = null;
            HTuple hv_LineColumnEnd_Measure_01_1 = null, hv_MsrHandle_Measure_01_1 = null;
            HTuple hv_LineRowStart_Measure_01_2 = null, hv_LineColumnStart_Measure_01_2 = null;
            HTuple hv_LineRowEnd_Measure_01_2 = null, hv_LineColumnEnd_Measure_01_2 = null;
            HTuple hv_MsrHandle_Measure_01_2 = null, hv_LineRowStart_Measure_01_3 = null;
            HTuple hv_LineColumnStart_Measure_01_3 = null, hv_LineRowEnd_Measure_01_3 = null;
            HTuple hv_LineColumnEnd_Measure_01_3 = null, hv_MsrHandle_Measure_01_3 = null;
            HTuple hv_LineRowStart_Measure_01_4 = null, hv_LineColumnStart_Measure_01_4 = null;
            HTuple hv_LineRowEnd_Measure_01_4 = null, hv_LineColumnEnd_Measure_01_4 = null;
            HTuple hv_MsrHandle_Measure_01_4 = null, hv_LineRowStart_Measure_01_5 = null;
            HTuple hv_LineColumnStart_Measure_01_5 = null, hv_LineRowEnd_Measure_01_5 = null;
            HTuple hv_LineColumnEnd_Measure_01_5 = null, hv_MsrHandle_Measure_01_5 = null;
            HTuple hv_Row_Measure_01_0 = null, hv_Column_Measure_01_0 = null;
            HTuple hv_Amplitude_Measure_01_0 = null, hv_Distance_Measure_01_0 = null;
            HTuple hv_Row_Measure_01_1 = null, hv_Column_Measure_01_1 = null;
            HTuple hv_Amplitude_Measure_01_1 = null, hv_Distance_Measure_01_1 = null;
            HTuple hv_Row_Measure_01_2 = null, hv_Column_Measure_01_2 = null;
            HTuple hv_Amplitude_Measure_01_2 = null, hv_Distance_Measure_01_2 = null;
            HTuple hv_Row_Measure_01_3 = null, hv_Column_Measure_01_3 = null;
            HTuple hv_Amplitude_Measure_01_3 = null, hv_Distance_Measure_01_3 = null;
            HTuple hv_Row_Measure_01_4 = null, hv_Column_Measure_01_4 = null;
            HTuple hv_Amplitude_Measure_01_4 = null, hv_Distance_Measure_01_4 = null;
            HTuple hv_Row_Measure_01_5 = null, hv_Column_Measure_01_5 = null;
            HTuple hv_Amplitude_Measure_01_5 = null, hv_Distance_Measure_01_5 = null;
            HTuple hv_Column_World_Measure_01_0 = null, hv_Row_World_Measure_01_0 = null;
            HTuple hv_TmpCtrl_Length = null, hv_TmpCtrl_RowFrom = new HTuple();
            HTuple hv_TmpCtrl_ColumnFrom = new HTuple(), hv_TmpCtrl_RowTo = new HTuple();
            HTuple hv_TmpCtrl_ColumnTo = new HTuple(), hv_Distance_World_Measure_01_0 = new HTuple();
            HTuple hv_Column_World_Measure_01_1 = null, hv_Row_World_Measure_01_1 = null;
            HTuple hv_Distance_World_Measure_01_1 = new HTuple(), hv_Column_World_Measure_01_2 = null;
            HTuple hv_Row_World_Measure_01_2 = null, hv_Distance_World_Measure_01_2 = new HTuple();
            HTuple hv_Column_World_Measure_01_3 = null, hv_Row_World_Measure_01_3 = null;
            HTuple hv_Distance_World_Measure_01_3 = new HTuple(), hv_Column_World_Measure_01_4 = null;
            HTuple hv_Row_World_Measure_01_4 = null, hv_Distance_World_Measure_01_4 = new HTuple();
            HTuple hv_Column_World_Measure_01_5 = null, hv_Row_World_Measure_01_5 = null;
            HTuple hv_Distance_World_Measure_01_5 = new HTuple(), hv_num = null;
            HTuple hv_num1 = null, hv_distance1 = new HTuple(), hv_distance2 = new HTuple();
            HTuple hv_distance3 = new HTuple(), hv_distance4 = new HTuple();
            HTuple hv_distance5 = new HTuple(), hv_distance6 = new HTuple();
            //HTuple hv_distance = new HTuple(), hv_distance_banchang = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image20180517163722);
            HOperatorSet.GenEmptyObj(out ho_ROI_0);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced1);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_Rectangle);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans1);
            HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_ImageInvert);
            HOperatorSet.GenEmptyObj(out ho_ImageFilter);
            HOperatorSet.GenEmptyObj(out ho_ImageFFT);
            HOperatorSet.GenEmptyObj(out ho_ImageConvol);
            HOperatorSet.GenEmptyObj(out ho_ImageFFT1);
            HOperatorSet.GenEmptyObj(out ho_Regions1);
            HOperatorSet.GenEmptyObj(out ho_Cross);
            HOperatorSet.GenEmptyObj(out ho_Image);
            ho_Image20180517163722.Dispose();

            ho_Image20180517163722 = h;
            //HOperatorSet.ReadImage(out ho_Image20180517163722, "C:/Users/Administrator/Desktop/Image__2018-05-17__17-51-32.bmp");
            ho_ROI_0.Dispose();
            HOperatorSet.GenCircle(out ho_ROI_0, 921.5, 1273.5, 1070.36);
            ho_ImageReduced1.Dispose();
            HOperatorSet.ReduceDomain(ho_Image20180517163722, ho_ROI_0, out ho_ImageReduced1
                );
            ho_Regions.Dispose();
            HOperatorSet.Threshold(ho_ImageReduced1, out ho_Regions, 0, 205);
            HOperatorSet.SmallestRectangle2(ho_Regions, out hv_Row, out hv_Column, out hv_Phi1,
                out hv_Length1, out hv_Length2);
            ho_Rectangle.Dispose();
            HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row, hv_Column, hv_Phi1, hv_Length1,
                hv_Length2);
            ho_RegionDilation.Dispose();
            HOperatorSet.DilationRectangle1(ho_Rectangle, out ho_RegionDilation, 11, 11);
            HOperatorSet.AreaCenter(ho_RegionDilation, out hv_Area1, out hv_Row2, out hv_Column2);
            HOperatorSet.OrientationRegion(ho_RegionDilation, out hv_Phi2);
            HOperatorSet.VectorAngleToRigid(hv_Row2, hv_Column2, hv_Phi1, hv_Row2, hv_Column2,
                (new HTuple(0)).TupleRad(), out hv_HomMat2D1);
            ho_ImageAffineTrans1.Dispose();
            HOperatorSet.AffineTransImage(ho_ImageReduced1, out ho_ImageAffineTrans1, hv_HomMat2D1,
                "constant", "false");
            ho_RegionAffineTrans.Dispose();
            HOperatorSet.AffineTransRegion(ho_RegionDilation, out ho_RegionAffineTrans, hv_HomMat2D1,
                "nearest_neighbor");
            ho_ImageReduced.Dispose();
            HOperatorSet.ReduceDomain(ho_ImageAffineTrans1, ho_RegionAffineTrans, out ho_ImageReduced
                );
            HOperatorSet.GetImageSize(ho_ImageReduced, out hv_Width, out hv_Height);
            ho_ImageInvert.Dispose();
            HOperatorSet.InvertImage(ho_ImageReduced, out ho_ImageInvert);
            ho_ImageFilter.Dispose();
            HOperatorSet.GenSinBandpass(out ho_ImageFilter, 0.4, "none", "rft", hv_Width,
                hv_Height);
            ho_ImageFFT.Dispose();
            HOperatorSet.RftGeneric(ho_ImageInvert, out ho_ImageFFT, "to_freq", "none", "complex",
                hv_Width);
            ho_ImageConvol.Dispose();
            HOperatorSet.ConvolFft(ho_ImageFFT, ho_ImageFilter, out ho_ImageConvol);
            ho_ImageFFT1.Dispose();
            HOperatorSet.RftGeneric(ho_ImageConvol, out ho_ImageFFT1, "from_freq", "n", "byte",
                hv_Width);
            ho_Regions1.Dispose();
            HOperatorSet.Threshold(ho_ImageFFT1, out ho_Regions1, 22, 236);
            ho_Cross.Dispose();
            HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row2, hv_Column2, 2500, 0);


            //Measure 01: Code generated by Measure 01
            //Measure 01: Initialize calibration
            hv_CameraParameters = new HTuple();
            hv_CameraParameters[0] = "area_scan_division";
            hv_CameraParameters[1] = 0.0125576;
            hv_CameraParameters[2] = -819.308;
            hv_CameraParameters[3] = 3.4492e-006;
            hv_CameraParameters[4] = 3.45e-006;
            hv_CameraParameters[5] = 1246.51;
            hv_CameraParameters[6] = 902.913;
            hv_CameraParameters[7] = 2448;
            hv_CameraParameters[8] = 2048;
            hv_CameraPose = new HTuple();
            hv_CameraPose[0] = 0.00169571;
            hv_CameraPose[1] = -0.00606483;
            hv_CameraPose[2] = 0.157227;
            hv_CameraPose[3] = 356.578;
            hv_CameraPose[4] = 358.764;
            hv_CameraPose[5] = 265.977;
            hv_CameraPose[6] = 0;
            //Measure 01: Prepare measurement
            hv_AmplitudeThreshold = 41;
            hv_RoiWidthLen2 = 35.5;
            HOperatorSet.SetSystem("int_zooming", "true");
            //Measure 01: Coordinates for line Measure 01 [0]
            hv_LineRowStart_Measure_01_0 = 1102.3;
            hv_LineColumnStart_Measure_01_0 = 579.963;
            hv_LineRowEnd_Measure_01_0 = 1100.95;
            hv_LineColumnEnd_Measure_01_0 = 1755.35;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_0 + hv_LineRowEnd_Measure_01_0);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_0 + hv_LineColumnEnd_Measure_01_0);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_0 - hv_LineRowEnd_Measure_01_0;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_0 - hv_LineColumnStart_Measure_01_0;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [0]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_0);
            //Measure 01: Coordinates for line Measure 01 [1]
            hv_LineRowStart_Measure_01_1 = 1010.2;
            hv_LineColumnStart_Measure_01_1 = 571.383;
            hv_LineRowEnd_Measure_01_1 = 1010.2;
            hv_LineColumnEnd_Measure_01_1 = 1752.49;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_1 + hv_LineRowEnd_Measure_01_1);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_1 + hv_LineColumnEnd_Measure_01_1);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_1 - hv_LineRowEnd_Measure_01_1;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_1 - hv_LineColumnStart_Measure_01_1;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [1]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_1);
            //Measure 01: Coordinates for line Measure 01 [2]
            hv_LineRowStart_Measure_01_2 = 1187.64;
            hv_LineColumnStart_Measure_01_2 = 569.953;
            hv_LineRowEnd_Measure_01_2 = 1187.64;
            hv_LineColumnEnd_Measure_01_2 = 1759.64;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_2 + hv_LineRowEnd_Measure_01_2);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_2 + hv_LineColumnEnd_Measure_01_2);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_2 - hv_LineRowEnd_Measure_01_2;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_2 - hv_LineColumnStart_Measure_01_2;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [2]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_2);
            //Measure 01: Coordinates for line Measure 01 [3]
            hv_LineRowStart_Measure_01_3 = 513.913;
            hv_LineColumnStart_Measure_01_3 = 1061.68;
            hv_LineRowEnd_Measure_01_3 = 1746.51;
            hv_LineColumnEnd_Measure_01_3 = 1061.68;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_3 + hv_LineRowEnd_Measure_01_3);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_3 + hv_LineColumnEnd_Measure_01_3);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_3 - hv_LineRowEnd_Measure_01_3;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_3 - hv_LineColumnStart_Measure_01_3;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [3]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_3);
            //Measure 01: Coordinates for line Measure 01 [4]
            hv_LineRowStart_Measure_01_4 = 526.23;
            hv_LineColumnStart_Measure_01_4 = 1279.34;
            hv_LineRowEnd_Measure_01_4 = 1698.42;
            hv_LineColumnEnd_Measure_01_4 = 1283.38;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_4 + hv_LineRowEnd_Measure_01_4);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_4 + hv_LineColumnEnd_Measure_01_4);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_4 - hv_LineRowEnd_Measure_01_4;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_4 - hv_LineColumnStart_Measure_01_4;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt()
                );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [4]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,
                hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_4);
            //Measure 01: Coordinates for line Measure 01 [5]
            hv_LineRowStart_Measure_01_5 = 516.511;
            hv_LineColumnStart_Measure_01_5 = 1173.8;
            hv_LineRowEnd_Measure_01_5 = 1711.69;
            hv_LineColumnEnd_Measure_01_5 = 1169.76;
            //Measure 01: Convert coordinates to rectangle2 type
            hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_5 + hv_LineRowEnd_Measure_01_5);
            hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_5 + hv_LineColumnEnd_Measure_01_5);
            hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_5 - hv_LineRowEnd_Measure_01_5;
            hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_5 - hv_LineColumnStart_Measure_01_5;
            hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
            hv_TmpCtrl_Len1 = 0.5 * ((((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc))).TupleSqrt() );
            hv_TmpCtrl_Len2 = hv_RoiWidthLen2.Clone();
            //Measure 01: Create measure for line Measure 01 [5]
            //Measure 01: Attention: This assumes all images have the same size!
            HOperatorSet.GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi,  hv_TmpCtrl_Len1, hv_TmpCtrl_Len2, 2448, 2048, "nearest_neighbor", out hv_MsrHandle_Measure_01_5);
            //Measure 01: ***************************************************************
            //Measure 01: * The code which follows is to be executed once / measurement *
            //Measure 01: ***************************************************************
            //Measure 01: The image is assumed to be made available in the
            //Measure 01: variable last displayed in the graphics window
            ho_Image.Dispose();
            HOperatorSet.CopyObj(ho_ImageAffineTrans1, out ho_Image, 1, 1);
            //Measure 01: Execute measurements
            HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_0, 2.6, hv_AmplitudeThreshold,"all", "all", out hv_Row_Measure_01_0, out hv_Column_Measure_01_0, out hv_Amplitude_Measure_01_0,out hv_Distance_Measure_01_0);
            HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_1, 2.6, hv_AmplitudeThreshold,"all", "all", out hv_Row_Measure_01_1, out hv_Column_Measure_01_1, out hv_Amplitude_Measure_01_1, out hv_Distance_Measure_01_1);
            HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_2, 2.6, hv_AmplitudeThreshold, "all", "all", out hv_Row_Measure_01_2, out hv_Column_Measure_01_2, out hv_Amplitude_Measure_01_2, out hv_Distance_Measure_01_2);
            HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_3, 2.6, hv_AmplitudeThreshold,"all", "all", out hv_Row_Measure_01_3, out hv_Column_Measure_01_3, out hv_Amplitude_Measure_01_3, out hv_Distance_Measure_01_3);
            HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_4, 2.6, hv_AmplitudeThreshold, "all", "all", out hv_Row_Measure_01_4, out hv_Column_Measure_01_4, out hv_Amplitude_Measure_01_4, out hv_Distance_Measure_01_4);
            HOperatorSet.MeasurePos(ho_Image, hv_MsrHandle_Measure_01_5, 2.6, hv_AmplitudeThreshold, "all", "all", out hv_Row_Measure_01_5, out hv_Column_Measure_01_5, out hv_Amplitude_Measure_01_5, out hv_Distance_Measure_01_5);
            //Measure 01: Transform to world coordinates
            //Measure 01: Calibrate positions for Measure 01 [0]
            HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_0,
                hv_Column_Measure_01_0, 0.001, out hv_Column_World_Measure_01_0, out hv_Row_World_Measure_01_0);
            //Measure 01: Calibrate distances
            hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_0.TupleLength());
            if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
            {
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_0, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_RowFrom);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_0, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_ColumnFrom);
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_0, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_RowTo);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_0, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_ColumnTo);
                HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                    hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_0);
            }
            //Measure 01: Calibrate positions for Measure 01 [1]
            HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_1, hv_Column_Measure_01_1, 0.001, out hv_Column_World_Measure_01_1, out hv_Row_World_Measure_01_1);
            //Measure 01: Calibrate distances
            hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_1.TupleLength());
            if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
            {
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_1, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_RowFrom);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_1, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_ColumnFrom);
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_1, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_RowTo);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_1, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_ColumnTo);
                HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                    hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_1);
            }
            //Measure 01: Calibrate positions for Measure 01 [2]
            HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_2, hv_Column_Measure_01_2, 0.001, out hv_Column_World_Measure_01_2, out hv_Row_World_Measure_01_2);
            //Measure 01: Calibrate distances
            hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_2.TupleLength());
            if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
            {
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_2, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_RowFrom);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_2, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_ColumnFrom);
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_2, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_RowTo);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_2, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_ColumnTo);
                HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                    hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_2);
            }
            //Measure 01: Calibrate positions for Measure 01 [3]
            HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_3,  hv_Column_Measure_01_3, 0.001, out hv_Column_World_Measure_01_3, out hv_Row_World_Measure_01_3);
            //Measure 01: Calibrate distances
            hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_3.TupleLength());
            if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
            {
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_3, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_RowFrom);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_3, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_ColumnFrom);
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_3, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_RowTo);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_3, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_ColumnTo);
                HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                    hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_3);
            }
            //Measure 01: Calibrate positions for Measure 01 [4]
            HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_4,  hv_Column_Measure_01_4, 0.001, out hv_Column_World_Measure_01_4, out hv_Row_World_Measure_01_4);
            //Measure 01: Calibrate distances
            hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_4.TupleLength());
            if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
            {
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_4, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_RowFrom);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_4, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_ColumnFrom);
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_4, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_RowTo);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_4, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_ColumnTo);
                HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                    hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_4);
            }
            //Measure 01: Calibrate positions for Measure 01 [5]
            HOperatorSet.ImagePointsToWorldPlane(hv_CameraParameters, hv_CameraPose, hv_Row_Measure_01_5, hv_Column_Measure_01_5, 0.001, out hv_Column_World_Measure_01_5, out hv_Row_World_Measure_01_5);
            //Measure 01: Calibrate distances
            hv_TmpCtrl_Length = new HTuple(hv_Row_World_Measure_01_5.TupleLength());
            if ((int)(new HTuple(hv_TmpCtrl_Length.TupleGreater(0))) != 0)
            {
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_5, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_RowFrom);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_5, 0, hv_TmpCtrl_Length - 2,
                    out hv_TmpCtrl_ColumnFrom);
                HOperatorSet.TupleSelectRange(hv_Row_World_Measure_01_5, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_RowTo);
                HOperatorSet.TupleSelectRange(hv_Column_World_Measure_01_5, 1, hv_TmpCtrl_Length - 1,
                    out hv_TmpCtrl_ColumnTo);
                HOperatorSet.DistancePp(hv_TmpCtrl_RowFrom, hv_TmpCtrl_ColumnFrom, hv_TmpCtrl_RowTo,
                    hv_TmpCtrl_ColumnTo, out hv_Distance_World_Measure_01_5);
            }
            //Measure 01: Do something with the results
            //Measure 01: Clear measure when done
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_0);
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_1);
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_2);
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_3);
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_4);
            HOperatorSet.CloseMeasure(hv_MsrHandle_Measure_01_5);










            hv_num = new HTuple(hv_Distance_World_Measure_01_0.TupleLength());
            hv_num1 = new HTuple(hv_Distance_World_Measure_01_5.TupleLength());
            if ((int)(new HTuple(hv_num.TupleEqual(hv_num1))) != 0)
            {
                hv_distance1 = hv_Distance_World_Measure_01_0.TupleSum();
                hv_distance2 = hv_Distance_World_Measure_01_1.TupleSum();
                hv_distance3 = hv_Distance_World_Measure_01_2.TupleSum();
                hv_distance4 = hv_Distance_World_Measure_01_3.TupleSum();
                hv_distance5 = hv_Distance_World_Measure_01_4.TupleSum();
                hv_distance6 = hv_Distance_World_Measure_01_5.TupleSum();
                hv_distance = (((((((hv_distance1 + hv_distance2) + hv_distance3) + hv_distance4) + hv_distance5) + hv_distance6)).TupleSum()
                    ) / 6;
                hv_distance_banchang = (hv_distance.TupleSum()) / 2;
                
                //MessageBox.Show("芯片长度：" + hv_distance + "mm\r\n" +
                //                "芯片半长: " + hv_distance_banchang +"mm");  
            }
            if ((int)(new HTuple(hv_num.TupleGreater(hv_num1))) != 0)
            {
                hv_distance1 = hv_Distance_World_Measure_01_0.TupleSum();
                hv_distance4 = hv_Distance_World_Measure_01_1.TupleSum();
                hv_distance5 = hv_Distance_World_Measure_01_2.TupleSum();
                hv_distance = ((((hv_distance1 + hv_distance4) + hv_distance5)).TupleSum()) / 3;
                hv_distance_banchang = (hv_distance.TupleSum()) / 2;

                //MessageBox.Show("芯片长度：" + hv_distance + "mm\r\n" +
                //                "芯片半长: " + hv_distance_banchang + "mm");
            }
            if ((int)(new HTuple(hv_num.TupleLess(hv_num1))) != 0)
            {
                hv_distance1 = hv_Distance_World_Measure_01_3.TupleSum();
                hv_distance4 = hv_Distance_World_Measure_01_4.TupleSum();
                hv_distance5 = hv_Distance_World_Measure_01_5.TupleSum();
                hv_distance = ((((hv_distance1 + hv_distance4) + hv_distance5)).TupleSum()) / 3;
                hv_distance_banchang = (hv_distance.TupleSum()) / 2;

                //MessageBox.Show("芯片长度：" + hv_distance + "mm\r\n" +
                //                "芯片半长: " + hv_distance_banchang + "mm");
            }


            ho_Image20180517163722.Dispose();
            ho_ROI_0.Dispose();
            ho_ImageReduced1.Dispose();
            ho_Regions.Dispose();
            ho_Rectangle.Dispose();
            ho_RegionDilation.Dispose();
            ho_ImageAffineTrans1.Dispose();
            ho_RegionAffineTrans.Dispose();
            ho_ImageReduced.Dispose();
            ho_ImageInvert.Dispose();
            ho_ImageFilter.Dispose();
            ho_ImageFFT.Dispose();
            ho_ImageConvol.Dispose();
            ho_ImageFFT1.Dispose();
            ho_Regions1.Dispose();
            ho_Cross.Dispose();
            ho_Image.Dispose();

        }
    }
}
